
# 购买流程类
from pages.client_page.goods_page import GoodsPage
from pages.client_page.login_page import LoginPage
from pages.client_page.settle_page import SettlePage
from pages.client_page.orders_page import OrdersPage
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
from loguru import logger
from utils.object_utils import cache


class BuyFlowLogic():

    # 购买流程
    def buy(self,driver):

        # 1. 点击首页
        driver.find_element(*LoginPage.my_page_elem).click()
        # 2. 点击商品
        driver.find_elements(*GoodsPage.good_detail)[0].click()
        sleep(3)

        # 获取商品名称，商品单价，商品数量
        good_name = driver.find_element(*GoodsPage.good_name).text
        logger.info("从商品详情页面获取到的商品名称:{}".format(good_name))
        cache.set("good_name",good_name[0:13])

        good_price = driver.find_element(*GoodsPage.good_price).text
        logger.info("从商品详情页面获取到的商品价格:{}".format(good_price))
        cache.set("good_price",good_price)

        good_num = driver.find_element(*GoodsPage.good_num).get_attribute("value")
        logger.info("从商品详情页面获取到的商品数量:{}".format(good_num))
        cache.set("good_num",good_num)

        # 3.加入购物车
        driver.find_element(*GoodsPage.add_cart).click()
        sleep(5)
        # 4. 关闭对话框
        driver.find_element(*GoodsPage.close_cart_dialog).click()
        driver.implicitly_wait(10)
        # 5. 点击我的购物车
        driver.find_element(*GoodsPage.my_cart).click()
        sleep(5)

        # 6. 点击去结算
        driver.find_element(*SettlePage.settle).click()
        sleep(10)
        # 7. 点击提交订单
        driver.find_element(*SettlePage.sumbit_order).click()
        driver.implicitly_wait(10)


        # 8. 选择货到付款
        driver.find_element(*SettlePage.pay_way).click()
        driver.implicitly_wait(10)

        # 9. 确认付款
        driver.find_element(*SettlePage.confirm_pay).click()
        sleep(1)

        # 10 .鼠标悬停在我的商场链接上
        ActionChains(driver).move_to_element(driver.find_element(*OrdersPage.my_shop)).perform()
        sleep(1)

        # 11. 点击我的订单
        driver.find_element(*OrdersPage.my_order).click()
        driver.implicitly_wait(5)


    # 获取商品信息
    def get_shop_info(self,driver):

        # 切换窗口
        driver.switch_to.window(driver.window_handles[-1])
        sleep(2)

        # 获取订单编号
        order_id = driver.find_elements(*OrdersPage.order_id)[1].text
        logger.info("获取到的订单id为:{}".format(order_id))

        # 获取商品名称
        shop_name = driver.find_elements(*OrdersPage.shop_name)[0].text
        logger.info("获取到的商品名称是:{}".format(shop_name))

        # 获取到的商品单价
        shop_price = driver.find_elements(*OrdersPage.shop_price)[1].text
        logger.info("获取到的商品单价是:{}".format(shop_price))

        # 获取商品数量
        shop_num = driver.find_elements(*OrdersPage.shop_num)[1].text
        logger.info("获取到的商品数量是:{}".format(shop_num))

        # 获取商品状态
        shop_status = driver.find_elements(*OrdersPage.shop_status)[2].text
        logger.info("获取到的商品状态是:{}".format(shop_status))

        return order_id,shop_name,shop_price,shop_num,shop_status


